package com.guoxin.mashibing.concurrent;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class LinkedBlockingQueueTest {
    static LinkedBlockingQueue<String> strs = new LinkedBlockingQueue<>();
    static Random r = new Random();

    public static void main(String[] args) {
        new Thread("P1") {
            @Override
            public void run() {

                for (int i = 0; i < 100; i++) {
                    try {
                        strs.put("a" + i);
                        TimeUnit.MICROSECONDS.sleep(r.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        for (int i = 0; i < 5; i++) {
            new Thread("C" + i) {
                @Override
                public void run() {
                    while(true){
                        try {
                            System.out.println(Thread.currentThread().getName()+"-"+strs.take());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }
}
